﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Reflection;

namespace QL_Truong_Hoc.HocTap
{
    public partial class frmChuyenLopNghe : DevExpress.XtraEditors.XtraForm
    {
        private const string mcstrRoleCode = "lop_hoc";
        private int mintPreLopID = clsConst.gcintNoneValue;

        public frmChuyenLopNghe()
        {
            InitializeComponent();
        }

        private void frmChuyenLopNghe_Load(object sender, EventArgs e)
        {
            if (!clsCommon.fncCheckRole(Program.glstRoles, mcstrRoleCode))
            {
                this.BeginInvoke(new MethodInvoker(this.Close));
                return;
            }

            // TODO: This line of code loads data into the 'qLTHDataSet.mNghe' table. You can move, or remove it, as needed.
            this.mNgheTableAdapter.Fill(this.qLTHDataSet.mNghe);
            // TODO: This line of code loads data into the 'qLTHDataSet.mKhoaHoc' table. You can move, or remove it, as needed.
            this.mKhoaHocTableAdapter.Fill(this.qLTHDataSet.mKhoaHoc);
            // TODO: This line of code loads data into the 'qLTHDataSet.mKhoa' table. You can move, or remove it, as needed.
            this.mKhoaTableAdapter.Fill(this.qLTHDataSet.mKhoa);
            // TODO: This line of code loads data into the 'qLTHDataSet.mTrinhDoDaoTao' table. You can move, or remove it, as needed.
            this.mTrinhDoDaoTaoTableAdapter.Fill(this.qLTHDataSet.mTrinhDoDaoTao);
            // TODO: This line of code loads data into the 'qLTHDataSet.mLopSelectAllWithDetail' table. You can move, or remove it, as needed.
            this.mLopSelectAllWithDetailTableAdapter.Fill(this.qLTHDataSet.mLopSelectAllWithDetail);

            dgvHsDen.BestFitColumns();
            dgvHsDi.BestFitColumns();
            txtHocSinh.Focus();
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            try
            {
                if (!vldCheck.Validate(txtHocSinh)) return;

                string strSearch = "";
                DataTable objData = null;
                strSearch = txtHocSinh.Text.Trim();

                objData = DAO.clsDbAccess.fncSearchHocSinh(Program.gobjDb, strSearch, false);
                grdControlHsDi.DataSource = objData;
                dgvHsDi.BestFitColumns();
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void lokLop_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                int intLopID;

                intLopID = clsCommon.fncCnvNullToInt(lokLop.EditValue);

                if (intLopID != mintPreLopID)
                {
                    mintPreLopID = intLopID;
                    this.tHocSinhWithLopTableAdapter.Fill(this.qLTHDataSet.tHocSinhWithLop, intLopID);
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }

        private void btnChuyen_Click(object sender, EventArgs e)
        {
            try
            {
                int intHsID;
                int intLopID_Di;
                int intLopID_Den;
                int intKDT_Di;
                int intKDT_Den;
                List<DTO.MKhungDaoTaoMonDTO> objMon_Di = new List<DTO.MKhungDaoTaoMonDTO>();
                List<DTO.MKhungDaoTaoMonDTO> objMon_Den = new List<DTO.MKhungDaoTaoMonDTO>();
                bool blnSuccess = true;
                DTO.THocSinhMonDTO objHsMon;
                string strHodem, strTen;
                string strKhoaDi, strLopDi;
                string strKhoaDen, strLopDen;
                int intTD_Di, intTD_Den;
                int intKhoas_Di, intKhoas_Den;


                //nếu chưa chọn lớp đến thì thoát
                intLopID_Den = clsCommon.fncCnvNullToInt(lokLop.EditValue);
                if (intLopID_Den <= 0)
                {
                    clsCommon.fncMsgWarning("Cần phải chọn lớp chuyển đến.");
                    return;
                }

                //nếu cùng 1 lớp thì không làm gì
                intLopID_Di = clsCommon.fncCnvNullToInt(dgvHsDi.GetFocusedRowCellValue(colLop_ID));
                if (intLopID_Den == intLopID_Di)
                {
                    clsCommon.fncMsgError("Chuyển lớp không thành công. Lớp đi và lớp đến phải khác nhau.");
                    return;
                }

                //nếu khác trình độ đào tạo hay khóa thì không cho chuyển
                intTD_Di = clsCommon.fncCnvNullToInt(dgvHsDi.GetFocusedRowCellValue(colTD_ID));
                intKhoas_Di = clsCommon.fncCnvNullToInt(dgvHsDi.GetFocusedRowCellValue(colKhoas_ID_Di));
                intTD_Den = clsCommon.fncCnvNullToInt(cbChonLop.GetFocusedRowCellValue(colTD_ID_Den));
                intKhoas_Den = clsCommon.fncCnvNullToInt(cbChonLop.GetFocusedRowCellValue(colKHS_ID_Den));
                if(intTD_Di != intTD_Den || intKhoas_Di != intKhoas_Den)
                {
                    clsCommon.fncMsgError("Chuyển lớp không thành công do khác trình độ đào tạo hoặc khác khóa.");
                    return;
                }

                strHodem = clsCommon.fncCnvNullToString(dgvHsDi.GetFocusedRowCellValue(colHoDem));
                strTen = clsCommon.fncCnvNullToString(dgvHsDi.GetFocusedRowCellValue(colTenHS));
                strKhoaDi = clsCommon.fncCnvNullToString(dgvHsDi.GetFocusedRowCellValue(colKhoas));
                strLopDi = clsCommon.fncCnvNullToString(dgvHsDi.GetFocusedRowCellValue(colLop));
                strLopDen = clsCommon.fncCnvNullToString(cbChonLop.GetFocusedRowCellValue(colLop_ten));

                //xác nhận
                if (!clsCommon.fncMsgConfirm(string.Format("Bạn có muốn chuyển học sinh {0} {1} từ lớp {2} sang lớp {3} không?", strHodem, strTen, strLopDi, strLopDen)))
                    return;

                //lấy id học sinh
                intHsID = clsCommon.fncCnvNullToInt(dgvHsDi.GetFocusedRowCellValue(colHs_ID));
                intKDT_Di = clsCommon.fncCnvNullToInt(dgvHsDi.GetFocusedRowCellValue(colKDT_ID_Di));
                intKDT_Den = clsCommon.fncCnvNullToInt(cbChonLop.GetFocusedRowCellValue(colKDT_ID));
                objHsMon = new DTO.THocSinhMonDTO();
                objHsMon.HS_ID = intHsID;

                //nếu chung khung đào tạo (hệ-khoa-khóa-nghề) thì chỉ chuyển lớp, môn học không đổi
                if (intKDT_Di != intKDT_Den)
                {
                    objMon_Di = DAO.MKhungDaoTaoMonDAO.SelectWhere(Program.gobjDb, "KDT_ID = " + intKDT_Di);
                    objMon_Den = DAO.MKhungDaoTaoMonDAO.SelectWhere(Program.gobjDb, "KDT_ID = " + intKDT_Den);
                }

                Program.gobjTrans = Program.gobjConn.BeginTransaction();

                try
                {
                    //xóa lớp cũ, thêm lớp mới.
                    blnSuccess = blnSuccess && DAO.TLopHocSinhDAO.Delete(Program.gobjDb, Program.gobjTrans, intLopID_Di, intHsID);
                    blnSuccess = blnSuccess && DAO.TLopHocSinhDAO.Insert(Program.gobjDb, Program.gobjTrans, new DTO.TLopHocSinhDTO(intLopID_Den, intHsID, 0));

                    if (intKDT_Di != intKDT_Den)
                    {
                        //xóa môn cũ
                        for (int i = 0; i <= objMon_Di.Count - 1; i++)
                        {
                            if (!blnSuccess) break;

                            blnSuccess = blnSuccess && DAO.THocSinhMonDAO.Delete(Program.gobjDb, Program.gobjTrans, intHsID, objMon_Di[i].MO_ID);
                        }

                        //thêm môn mới
                        for (int i = 0; i <= objMon_Den.Count - 1; i++)
                        {
                            if (!blnSuccess) break;
                            objHsMon.MO_ID = objMon_Den[i].MO_ID;
                            blnSuccess = blnSuccess && DAO.THocSinhMonDAO.Insert(Program.gobjDb, Program.gobjTrans, objHsMon);
                        }
                    }

                    if (blnSuccess) { Program.gobjTrans.Commit(); }
                    else { Program.gobjTrans.Rollback(); }
                }
                catch { Program.gobjTrans.Rollback(); }

                if (blnSuccess)
                {
                    dgvHsDi.DeleteSelectedRows();
                    dgvHsDen.AddNewRow();
                    dgvHsDen.SetFocusedRowCellValue(colLop_ID_HS_den, intLopID_Den);
                    dgvHsDen.SetFocusedRowCellValue(colhs_hodem, strHodem);
                    dgvHsDen.SetFocusedRowCellValue(colhs_ten, strTen);
                    dgvHsDen.UpdateCurrentRow();
                }
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }
    }
}